Skip to content

Conversation

sheerun
Copy link
Contributor

@sheerun sheerun commented Oct 10, 2018

Fixed version of previous PR, I now automatically shim not existing event types on window

src/events.js Outdated
}
const window = node.ownerDocument.defaultView
if (typeof window[EventType] === 'undefined') {
window[EventType] = class extends window.Event {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's relevant line, btw. Previously this events were shimmed only in document.js helper

// Clipboard Events
copy: {
EventType: ClipboardEvent,
EventType: 'ClipboardEvent',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably because of changes like this bundler didn't bother to shim things like ClipboardEvent (not that it should as such shimming won't work on custom jsdom instances, just on global window)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bundler doesn't shim anything.

kentcdodds
kentcdodds previously approved these changes Oct 10, 2018
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this locally with react-testing-library's tests and they passed. If this passes CI then I'll merge it.

Thanks for your work on this @sheerun 👍

@kentcdodds
Copy link
Member

I'm not sure why the test is breaking, but I do want to use window.MutationObserver if it's defined (for the in-browser use-case).

return element.dispatchEvent(event)
}

Object.entries(eventMap).forEach(([key, {EventType = Event, defaultInit}]) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were just defaulting to Event before due to EventType = Event in the function params above, when EventType was undefined (ie, InputEvent didn't exist before jsdom 12 so was undefined). Now that they are all changed to strings, like 'InputEvent', it's always defined so never defaulted over to 'Event'.

Your new fix of

if (typeof window[EventType] === 'undefined') {
        window[EventType] = class extends window.Event {}
}
const event = new window[EventType](eventName, eventInit);

works, but we could also just do something like

const eventType = window[EventType] || window.Event;
const event = new eventType(eventName, eventInit);

if we wanted to keep the same behavior of not shimming.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my changes address this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, cool. Bit behind on the code updates then ^_^
I definitely like name EventConstructor over a weird lowercased eventType.

@sheerun
Copy link
Contributor Author

sheerun commented Oct 12, 2018

The error was because document has no ownerDocument and it can be passed as a container.

I've fixed the code to account for this case.

@kentcdodds
Copy link
Member

We've got some merge conflicts. Could you look at those please?

timer = setTimeout(onTimeout, timeout)
observer = new MutationObserver(onMutation)
const window = container.ownerDocument.defaultView
const MutationObserverConstructor = window.MutationObserver || MutationObserver
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not necessary as @sheerun/mutationobserver-shim already uses global MutationObserver if available

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice! Yeah, when you work on the merge conflicts, could you fix that?

Thanks for all the work your putting into this 👍


function getDocument() {
if (typeof document === 'undefined') {
throw new Error('Could not find default container')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code defeats purpose of this PR as this while this PR adds ability for this library to work without global document, this code throws an error when so..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is a more elegant way of what you were doing before. To avoid running this code all you have to do is provide a container to waitForElement. If you don't, it'll throw the error (which is what was happening before).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ok it seems fine, I didn't realize JS evaluates default values lazily

@sheerun sheerun closed this Oct 12, 2018
@sheerun
Copy link
Contributor Author

sheerun commented Oct 12, 2018

I'll send another one shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants